﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using RssDataModel.ViewModel;

namespace RssDataModel
{
    class DataModelCustomizedActions
    {
    }

    public partial class DataClasses
    {
        /// <summary>
        /// testing purposes
        /// </summary>
        public List<String> getSelectedSiteNamesforUser(Int32 userID)
        {
            //var query = from us in userS
            //            join s in sites on us.siteID equals s.ID
            //            where us.userID == userID
            //            select s.Name;

            //List<String> l = query.ToList();
            return null;
        }

        /// <summary>
        /// get sites for userwith id "userID"
        /// </summary>
        /// <param name="userId"></param>
        public IEnumerable<SiteSelectItem> getSitesForUser(int userId)
        {
            IEnumerable<SiteSelectItem> query =
                from s in sites
                select new SiteSelectItem(s.ID,
                                        s.Logo,
                                        s.Description,
                                        s.Logo,
                                        (s.userSites.First() == null ? false : true));
            return query;
        }

  
        #region rss feed updater
        /// <summary>
        /// reads RSS feeds from all configured RSS sources
        /// </summary>
        /// <returns></returns>
        public IEnumerable<article> getArticlesFromRSSFeeds()
        {
            XElement root = null;
            IEnumerable<article> elquery = null;
            List<article> result = new List<article>();
            int idCount = 0;

            foreach (site s in this.sites)
            {
                try
                {
                    root = XElement.Load(s.RSSAddress);
                    elquery = from el in root.Descendants("item")
                              select new article
                              {
                                  ID = idCount++,
                                  siteID = s.ID,
                                  Title = el.Element("title") == null ? "" : el.Element("title").Value,
                                  Category = el.Element("category") == null ? "" : el.Element("category").Value,
                                  Description = el.Element("description") == null ? "" : el.Element("description").Value,
                                  Fullarticle = "",
                                  Weblink = el.Element("link") == null ? "" : el.Element("link").Value,
                                  Pubdate = el.Element("pubDate") == null ? DateTime.Now : Convert.ToDateTime(el.Element("pubDate").Value)
                              };
                }
                catch (Exception)
                {

                }
                result.AddRange(elquery);
            }
            return result;
        }

        /// <summary>
        /// add new articles to db; ignoring articles already in db
        /// </summary>
        /// <param name="newArticles"></param>
        public void addArticlesToDB(IEnumerable<article> newArticles)
        {
            List<article> articlesToAdd = new List<article>();

            foreach (article na in newArticles)
                if ((articles.Where(s => (s.Weblink.Equals(na.Weblink)))).Count() < 1)
                    articles.InsertOnSubmit(na);
            SubmitChanges();
        }

        /// <summary>
        /// delete articles from table article older than "articleAgeInHours"
        /// </summary>
        /// <param name="articleAgeInHours"></param>
        public void deleteArticlesOlderThan(int articleAgeInHours)
        {
            IEnumerable<article> articlesToDelete;

            //ToDo: le kellene ellenorizni a query-t!!!! nem biztos, hogy ok :(
            articlesToDelete = articles.Where(x => !userArticles.Select(y => y.articleID).Contains(x.ID))
                                        .Where(x => x.Pubdate.CompareTo(DateTime.Now.AddHours(-articleAgeInHours)) > 0);
            articles.DeleteAllOnSubmit(articlesToDelete);
            SubmitChanges();
        }
        #endregion
    }
}
